package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 品牌制造商列表
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    //http://localhost:8083/admin-ui/brand/list?page=1&limit=20&sort=add_time&order=desc
    public Object list(Integer id,String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {

        IPage<DtsBrand> brandIPage = brandService.queryByPage(
                id,name,page,limit,sort,order);
        //总条数
        long total = brandIPage.getTotal();
        //当前数据
        List<DtsBrand> records = brandIPage.getRecords();

        List<DtsCategory> dtsCategories = categoryService.showCat();
        //封装categoryIds
        for (int i = 0; i < records.size(); i++) {
            for (DtsCategory category : dtsCategories) {
                if (records.get(i).getDefaultCategoryId().equals(category.getId())){
                    Integer[] integers = new Integer[2];
                    integers[0] = category.getPid();
                    integers[1] = category.getId();
                    records.get(i).setCategoryIds(integers);
                }
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", records);

        return ResponseUtil.ok(map);
    }

    //下拉列表
    //品牌和管理员列表
    //http://localhost:8083/admin-ui/brand/catAndAdmin

    @GetMapping("/catAndAdmin")
    public Object ListCatAd() {
        List<DtsAdmin> admins = adminService.findAll();
        //封装admin
        List<CatVo> adminList = adminsVo(admins);

        List<CatVo> catVoList = brandService.list();

        List<DtsCategory> list = categoryService.list();
        //封装
        List<CatVo> categoryList = vo2(catVoList, list);

        Map<String, Object> map = new HashMap<>();
        map.put("adminList", adminList);
        map.put("categoryList", categoryList);
        return ResponseUtil.ok(map);
    }

    //封装admin
    private List<CatVo> adminsVo(List<DtsAdmin> admins) {
        List<CatVo> list = new ArrayList<>();
        for (int i = 0; i < admins.size(); i++) {
            CatVo catVo = new CatVo();
            catVo.setValue(admins.get(i).getId());
            catVo.setLabel(admins.get(i).getUsername());
            list.add(catVo);
        }
        return list;
    }

    //封装category和brand
    public List<CatVo> vo2(List<CatVo> catVoList, List<DtsCategory> list) {
        if (catVoList == null || list == null) {
            throw new RuntimeException("数据为空！");
        }

        for (int i = 0; i < catVoList.size(); i++) {
            List<CatVo> arrayList = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                CatVo catVo = new CatVo();
                if (catVoList.get(i).getValue().equals(list.get(j).getPid())) {
                    Integer id = list.get(j).getId();
                    String name = list.get(j).getName();
                    catVo.setValue(id);
                    catVo.setLabel(name);
                    arrayList.add(catVo);
                }
            }
            catVoList.get(i).setChildren(arrayList);
        }
        return catVoList;
    }

    //##### 品牌制造商添加
    //
    //- 接口地址 : http://localhost:8083/admin-ui/brand/create
    @PostMapping("/create")
    public Object create(@RequestBody DtsBrand brand) {
        DtsBrand dtsBrand = brandService.insertOne(brand);
        return ResponseUtil.ok(dtsBrand);
    }

    // http://localhost:8083/admin-ui/brand/update
    @RequestMapping("/update")
    public Object update(@RequestBody DtsBrand brand) {
        int updateOne = brandService.updateOne(brand);
        if (updateOne == 0){
            new RuntimeException("修改失败");
        }
        return ResponseUtil.ok(brand);
    }
        //http://localhost:8083/admin-ui/brand/delete
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        brandService.deleteOne(brand);
        return ResponseUtil.ok();
    }


}
